import { dialog, BrowserWindow, MessageBoxOptions, MessageBoxReturnValue } from 'electron';
import { FileExistsOperation } from '@p/file';
// import '@types/node';

interface Returns {
	learn: boolean;
	response: FileExistsOperation;
}

/**
 * 文件已存在对话框
 */
export function fileAlreadyExistDialog(win: BrowserWindow | undefined, filename: string, filePath?: string) {
	return new Promise<Returns>((resolve) => {
		let message = filename + ' 已存在';

		if (filePath) {
			message += `路径："${filePath}"`;
		}

		const options: MessageBoxOptions = {
			buttons: ['在文件名后面加序号', '覆盖', '取消'],
			message,
			title: '文件已存在',
			type: 'warning',
			checkboxLabel: '记住我的选择',
			defaultId: 0,
		};

		const callback = (obj: MessageBoxReturnValue) => {
			const resluts: FileExistsOperation[] = ['addNumber', 'cover', 'cancel'];

			resolve({
				learn: obj.checkboxChecked,
				response: resluts[obj.response],
			});
		};

		if (win) {
			dialog.showMessageBox(win, options).then(callback);
		} else {
			dialog.showMessageBox(options).then(callback);
		}
	});
}
